const date = new Date()
var app = getApp()
var that
Page({
  data: {
    currentTab: 0,
    currentTab_goods: 0,
    my_cat2: [], 
    my_cat:[],   //我的猫咪
    name:"用户名",
    sex:"男",
    user_head_picurl: "../../images/brk.jpg",
    cat_head_picurl: "../../images/MM.jpg",
    h:"", //剩余高度
    rr_msg:[],

    home_num:"",
    ser_num: "",
    shop_num: "",
    // hidden:false,  //显示加载中背景图层
  },
  onLoad: function () {
    that=this;
    wx.getSystemInfo({
      success: function (res) { that.setData({ h: res.windowHeight }); }
    });

    wx.showToast({
      title: '加载中', icon: 'loading', duration: 2000,
      // success: function () {
      //   that.setData({ hidden: true })
      // }
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },
  onShow: function () {
    app.check_vs(); //检查版本
    that.get_person_f_rr();//获取个人人人商城的信息//重新加载
  },
  addcat:function(){//点击添加猫的图片跳转到添加猫的界面
    wx.navigateTo({
      url: '../cat_mse/cat_mse',
    })
  },
  cat_detail:function(e){ //点击猫的头像跳转猫的详细信息
    wx.navigateTo({                  //将json对象转为字符串传过去
      url: '../cat_mse_look/cat_mse_look?cat_id=' + e.currentTarget.dataset.d,
    })
  },
  toperson:function(){   //点击用户头像跳到用户个人信息
    wx.navigateTo({
      url: './personal_mse'
    })
  },
  application:function(){
    wx.navigateTo({ url: '../member/invite_code?from=p_center' }); return
    if (app.data.person_msg_rr.mobile == "" || app.data.person_msg_rr.pwd == "") {
      wx.navigateTo({ url: '../member/invite_code?from=p_center' })  //无电话密码跳绑定电话密码
    } else {
      wx.navigateTo({ url: '../member/buy_cart' }) //有电话密码跳买卡
    }
  }, 
  set_up:function(){//点击设置跳到设置界面
    wx.navigateTo({
      url: '../set_up/set_up'
    })
  },
  setcat_msg:function(){
    for (var i = 0; i < that.data.my_cat.length;i++){
      if (that.data.my_cat[i].sex==1)
      {
        that.data.my_cat[i].sex="GG"
      } else { that.data.my_cat[i].sex = "MM" }
      var d = that.data.my_cat[i].birthday.split("-")
      that.data.my_cat[i].birthday =  that.cat_age(d[0],d[1],d[2]);
      that.setData({ my_cat2: that.data.my_cat});
    }
  },
  cat_age: function (y, m, d) {
    var n_year = date.getFullYear()
    var n_month = date.getMonth() + 1
    var n_day = date.getDate()
    var age
    if ((n_year - y) * 12 + (n_month - m) * 30 + (n_day - d) < 0) {
      //猫还没生下来呢
    } else {
      var mon = (n_year - y) * 12 + (n_month - m);
        age=((mon/12) - 0.5).toFixed(0) + "岁" + mon % 12 + "个月" ;
      if (((mon / 12) - 0.5).toFixed(0) <= 0) {
          age =mon % 12 + "个月" ;
      }
      return age;
    }
  },
  navigateToOrder: function (e) {
    if (that.data.currentTab_goods === e.currentTarget.dataset.status) {
      return false;
    } else {
      var cur = e.currentTarget.dataset.status;
      that.setData({
        currentTab_goods: parseInt(cur),
      });
    };
    var url='';
    if (e.currentTarget.dataset.otype == "care") { url ='../new_order/order_care'}
    if (e.currentTarget.dataset.otype == "ser") { url = '../new_order/order_ser' }
    if (e.currentTarget.dataset.otype == "shop") { url = '../new_order/order_shop' }
    wx.navigateTo({
      url: url + '?cur=' + e.currentTarget.dataset.status
    })
  },
  //改变选项卡
  bindChange: function (e) {
    that.setData({ currentTab: e.detail.current });
  },
  //选项卡标题绑定的事件
  swichNav: function (e) {
    if (that.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      var current = e.target.dataset.current;
      that.setData({
        currentTab: parseInt(current),
      });
    };
  },
  get_cat: function (openid){
    app.req("get_cat.html", { sessionid: wx.getStorageSync('3rd_session'), "openid": openid }, function (res) {
      if (res.status == "success") { //获取猫的信息
        that.setData({ my_cat: res.mess });
        that.setcat_msg();  //设置猫的各项信息
        
      } else if (res.status == "error") {
        that.setData({ my_cat: [], my_cat2: [] });
        app.toast_up(res.mess)//还未添加猫咪提示
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.get_cat(openid)
          }
        })
      }
    }) 
  },
  get_person_f_rr: function () { 
 
    app.get_msg_rr(function(){
      var openid = wx.getStorageSync('openid');
      var userInfo = wx.getStorageSync('userInfo');
      that.setData({ rr_msg: app.data.person_msg_rr })
      console.info('openid:' + openid);
      app.get_p_msg(openid)
      var res_d = userInfo;
    
     
      if (res_d.sex == 2) {   //设置用户名
        that.setData({ sex: "女" });
      }
      if (res_d.custom_nickname == "") {   //设置用户名
        that.setData({ name: res_d.nickname });
      } else { that.setData({ name: res_d.custom_nickname }); }
      if (res_d.custom_photo == "") { //设置用户头像
        that.setData({ user_head_picurl: res_d.photo });
      } else {
        setTimeout(function () {
          that.setData({ user_head_picurl: res_d.custom_photo })
        }, 100)
      }
      that.get_cat(openid)
      that.get_order_num(openid);
    }) 
  },
  get_order_num: function (openid){  //获取各类订单的数量
    app.req("get_order_num.html", { "openid": openid }, function (res) {
      if (res.status == "success") { //获取猫的信息
        
        that.setData({ home_num: res.mess.home[0], ser_num: res.mess.ser[0], shop_num: res.mess.shop[0]})
      } else if (res.status == "error") {
        that.setData({ my_cat: [], my_cat2: [] });
        app.toast(res.mess)//还未添加猫咪提示
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.get_order_num(openid)
          }
        })
      }
    })
  }
})